home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 676-700 / 687 / xsb / standard / latticelib.a < prev    next >
Text File  |  1995-03-18  |  7KB  |  204 lines

  1. ; Latticelib.a: Amiga shared runtime library shell for Lattice 'C' 5.02+
  2. ; By Rick Huebner, 28 October 1989.  Released to the Public Domain.
  3. ; Derived from sample.library.asm in ROM Kernal Reference Manual and other
  4. ; sources.
  5.  
  6. ; Provides RomTag and other magic thingies needed to make a set of
  7. ; C routines into a library.  Should work with most any set of routines;
  8. ; just substitute in the proper external labels.  This file should be the
  9. ; first one given in the link list, so that the RomTag struct can be quickly
  10. ; & easily found by the system when the library is opened.
  11.  
  12. ; 5.04 note: I just got done trying to use the new libent and libinit modules
  13. ; provided with Lattice C 5.04, and ran into a few problems.  I'm probably
  14. ; not using them quite right (the update docs are pretty sketchy), but I'm
  15. ; not going to waste any more time on them.  I'd already written and tested
  16. ; this stuff before 5.04 came, and it works well.  If it ain't broke...
  17.  
  18. ; --------------------
  19. ; made some changes to fit XSB purposes - O
  20.  
  21.  
  22.     INCLUDE    "exec/types.i"
  23.     INCLUDE    "exec/libraries.i"
  24.     INCLUDE    "exec/lists.i"
  25.     INCLUDE    "exec/initializers.i"
  26.     INCLUDE    "exec/resident.i"
  27.  
  28.     ; Things we define for external reference (for easier debuggging)
  29.     XDEF    LibStart
  30.     XDEF    RomTag
  31.     XDEF    LibName
  32.     XDEF    LibIDString
  33.     XDEF    LibInit
  34.     XDEF    LibOpen
  35.     XDEF    LibClose
  36.     XDEF    LibExpunge
  37.     XDEF    LibExtFunc
  38.     XDEF    LibEnd
  39.  
  40.     ; Our library base structure description
  41.     STRUCTURE XSBBase,LIB_SIZE
  42.     ULONG    xb_SegList
  43.     LABEL    XSBBase_SIZEOF
  44.  
  45.  
  46.     SECTION    text,code
  47.  
  48.     ; Things we need others to define
  49.     XREF    @XSBBlankScreen
  50.     XREF    @XSBGetID
  51.     XREF    @XSBGetParams
  52.     XREF    __BSSBAS
  53.     XREF    __BSSLEN
  54.     XREF    _AbsExecBase
  55.     XREF    _LVOOpenLibrary
  56.     XREF    _LVOCloseLibrary
  57.     XREF    _LVORemove
  58.     XREF    _LVOFreeMem
  59.  
  60.  
  61.     ; Supposed start of executable code.  This is where execution
  62.     ; will start if anybody's silly enough to try and run the library
  63.     ; from the command line.  Just return the highest error code we
  64.     ; conveniently can to tell them they screwed up.
  65. LibStart:
  66.     moveq    #-1,d0
  67.     rts
  68.  
  69.     ; Where the magic begins.  OpenLibrary actually looks through the
  70.     ; library file contents trying to find this table by locating the
  71.     ; magic RTC_MATCHWORD value followed by its own address.  This
  72.     ; table then tells OpenLibrary where to find other things it needs.
  73.     ; This needs to be close to the front of your library file to cut
  74.     ; down on the amount of searching OpenLibrary does; that's why
  75.     ; this object file should be first in the link list.
  76. RomTag:    dc.w    RTC_MATCHWORD    ; Magic value to search for to find table
  77.     dc.l    RomTag        ; Address of matchword; the two together prevent accidental matches
  78.     dc.l    LibEnd        ; Address of end of library handler code
  79.     dc.b    RTF_AUTOINIT    ; Request system to automatically initialize our library
  80.     dc.b    VERSION        ; Version number of our library (defined below)
  81.     dc.b    NT_LIBRARY    ; Node type = Library
  82.     dc.b    0        ; Node priority = 0 (normal)
  83.     dc.l    LibName        ; Name of this library file, for debugging info
  84.     dc.l    LibIDString    ; More debugging info
  85.     dc.l    inittable    ; Initialization table used by RTF_AUTOINIT
  86.  
  87. VERSION     EQU    1
  88. REVISION EQU    0
  89. LibName:
  90.     dc.b    "xsbstandard.library",0
  91.     dc.b    "$VER: "
  92. LibIDString:
  93.     dc.b    "xsbstandard 1.0 (17 feb 1992)",13,10,0
  94.  
  95.     ds.w    0
  96.  
  97. inittable:
  98.     dc.l    XSBBase_SIZEOF    ; Size of our library base struct
  99.     dc.l    functable    ; Where our function addresses are
  100.     dc.l    datatable    ; Initialization info for our library base struct
  101.     dc.l    LibInit        ; Library initialization routine address
  102.  
  103. functable:
  104.     dc.l    LibOpen            ; Addresses of all library functions
  105.     dc.l    LibClose        ; First 4 MUST be provided, in this order
  106.     dc.l    LibExpunge
  107.     dc.l    LibExtFunc
  108.     dc.l    @XSBBlankScreen    ; The meat of the library.
  109.     dc.l    @XSBGetID
  110.     dc.l    @XSBGetParams
  111.     dc.l    -1
  112.  
  113.     ; Things for the system to automatically initialize for us via RTF_AUTOINIT request
  114. datatable:
  115.     INITBYTE    LN_TYPE,NT_LIBRARY
  116.     INITLONG    LN_NAME,LibName
  117.     INITBYTE    LIB_FLAGS,LIBF_SUMUSED | LIBF_CHANGED
  118.     INITWORD    LIB_VERSION,VERSION
  119.     INITWORD    LIB_REVISION,REVISION
  120.     INITLONG    LIB_IDSTRING,LibIDString
  121.     dc.l    0
  122.  
  123.  
  124.     ; System interface routines.  Exec has performed Forbid() before
  125.     ; getting here, so do this stuff quick and get the hell out.
  126.  
  127.  
  128.     ; Routine which is executed by the system as part of first OpenLibrary
  129.     ; call, when the library is first loaded into RAM from disk.
  130.     ; D0 = library base pointer, A0 = segment list pointer.  Must return
  131.         ; nonzero in D0 if initialization worked, or zero if it failed.
  132. LibInit:
  133.     move.l    d0,-(sp)        ; Save D0 for return code
  134.     move.l    d0,a1            ; A1 = library base
  135.     move.l    a0,xb_SegList(a1)    ; Save seglist addr for future expunge
  136.     lea    __BSSBAS,a0        ; Initialize BSS memory to 0s
  137.     move.l    #__BSSLEN,d0
  138.     moveq    #0,d1
  139.     bra.s    clr_lp
  140. clr_bss    move.l    d1,(a0)+
  141. clr_lp    dbf    d0,clr_bss
  142.     move.l    (sp)+,d0        ; Return nonzero = success
  143.     rts
  144.  
  145.     ; Open the library.  Executed by system as part of OpenLibrary call,
  146.     ; after loading the library into RAM and calling LibInit if necessary.
  147.     ; D0 = version, A6 = library base.  Return nonzero if open worked,
  148.     ; or zero if open failed for some reason.
  149. LibOpen:
  150.     addq.w    #1,LIB_OPENCNT(a6)        ; Increment open count
  151.     bclr    #LIBB_DELEXP,LIB_FLAGS(a6)    ; Clear delayed expunge flag
  152.     move.l    a6,d0                ; Return nonzero = success
  153.     rts
  154.  
  155.     ; Close the library.  Executed by system as part of CloseLibrary call.
  156.     ; A6 = library base.  Return seglist address if a delayed expunge
  157.     ; was performed, else return 0 if library is still loaded.
  158. LibClose:
  159.     moveq    #0,d0                ; Init return value = 0
  160.     subq.w    #1,LIB_OPENCNT(a6)        ; Decrement open count
  161.     bne.s    dontexpunge            ; If still open by others, can't expunge
  162.     btst    #LIBB_DELEXP,LIB_FLAGS(a6)    ; Was an expunge requested while we were open?
  163.     beq.s    dontexpunge
  164.     bsr.s    LibExpunge            ; If so, do it now
  165. dontexpunge:
  166.     rts
  167.  
  168.     ; Expunge the library (remove it from RAM).  Executed by system
  169.     ; memory allocation routine when memory gets tight.  A6 = library
  170.     ; base.  Return seglist address if library was closed and we were
  171.     ; able to expunge, else return 0 if library is still loaded.
  172. LibExpunge:
  173.     movem.l    d2/a5/a6,-(sp)
  174.     move.l    a6,a5
  175.     move.l    _AbsExecBase,a6            ; Get ExecBase for future use
  176.     tst.w    LIB_OPENCNT(a5)            ; Is library open?
  177.     beq.s    doexpunge
  178.     bset    #LIBB_DELEXP,LIB_FLAGS(a5)    ; If so, set delayed expunge flag
  179.     moveq    #0,d0                ; and return 0
  180.     bra.s    expungedone
  181. doexpunge:
  182.     move.l    xb_SegList(a5),d2        ; Save seglist address in D2
  183.     move.l    a5,a1                ; A1 = library base address
  184.     jsr    _LVORemove(a6)            ; Unlink library node from system library list
  185.     moveq    #0,d0                ; Compute total size of library node
  186.     move.w    LIB_NEGSIZE(a5),d0        ;   D0 = bytes used before base
  187.     move.l    a5,a1
  188.     sub.l    d0,a1                ;   A1 = base - negsize
  189.     add.w    LIB_POSSIZE(a5),d0        ;   D0 = possize + negsize
  190.     jsr    _LVOFreeMem(a6)            ; Free library node memory
  191.     move.l    d2,d0                ; Return segment list address
  192. expungedone:
  193.     movem.l    (sp)+,d2/a5/a6
  194.     rts
  195.  
  196.     ; "Extra" function (room for future growth?).  We don't need it,
  197.     ; so dummy it out.
  198. LibExtFunc:
  199.     moveq    #0,d0
  200.     rts
  201.  
  202. LibEnd:
  203.     END
  204.